// pages/offlinePay/offlinePay.js
const app = getApp();
import api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    venuesList: [],
    venuesIndex: 0,
    venuesIdList: [],

    examItem: {},
    infoItem: {},
    isShowExaminee: true,
    isShowVenues: true,
    price: 1.00,
    
    // exam_address_id: null
  },

  goToMyInfo(e){
    wx.navigateTo({
      url: '/pages/myApplyInfo/myApplyInfo',
    })
  },

  // 选择考点
  changeVenues(e){
    let venuesIndex = parseInt(e.detail.value)
    this.setData({
      venuesIndex,
      isShowVenues: false,
    })
    console.log(this.data.venuesList[venuesIndex])
  },

  // 获取考点列表
  getVenuesList(){

    api.POST('/applet/examAddress/findAddressList', {
      "exam_id": this.data.examItem.exam_id
    }, res => {
      console.log(res.data.data)
      // this.setData({
      //   exam_address_id: res.data.data.exam_address_id
      // })
      let list = res.data.data
      let venuesList = []
      let venuesIdList = []
      for (let i = 0; i < list.length; i++) {

        venuesIdList[i] = list[i].exam_address_id

        if (!list[i].city_name && !list[i].area_name){
          venuesList[i] = list[i]['province_name']
        } else if(list[i].city_name || list[i].area_name) {
          if (list[i].city_name && list[i].area_name){
            venuesList[i] = list[i]['province_name'] + list[i]['city_name'] + list[i]['area_name']
          } else if (list[i].city_name && !list[i].area_name){
            venuesList[i] = list[i]['province_name'] + list[i]['city_name']
          } else if (!list[i].city_name && list[i].area_name){
            venuesList[i] = list[i]['province_name'] + list[i]['area_name']            
          }
        }
      }

      this.setData({
        venuesList,
        venuesIdList
      })
    })
    
  },

  //创建订单
  goToCreateOrder() {
    if (this.data.isShowExaminee) {
      wx.showToast({
        title: '请添加考试人',
        icon: 'none'
      })
      return
    }
    if (this.data.isShowVenues) {
      wx.showToast({
        title: '请选择考点',
        icon: 'none'
      })
      return
    }

    console.log(app.globalData.examItem)

    // let addressInfo = this.data.dafaultAddress
    let params = {}

    params.exam_id = this.data.examItem.exam_id
    params.total_price = this.data.examItem.offline_price
    params.exam_address_id = this.data.venuesIdList[this.data.venuesIndex]
    params.buyer_name = this.data.infoItem.real_name
    params.buyer_phone = this.data.infoItem.mobile
    params.exam_status = 2

    console.log(params)
    api.POST('/applet/exam_order/create', params, res => {
      console.log(res)
      this.setData({
        exam_order_id: res.data.order_id
      })

      // let obj = {}
      // obj.order_identifier = res.data.exam_order_id

      // api.POST('/applet/exam_order/checkExamPermit', obj, res => {
      //   // 准考证
      //   console.log(res.data)
      //   app.globalData.card = res.data
      // })

      // api.POST('/applet/ContactService/detail', {}, res => {
      //   app.globalData.QR = res.data.exam_card_url
      // })

      this.pay(res.data.exam_order_id)
    })
  },


  pay(id) {
    const that = this;
    api.POST('applet/customer/jsApiPay', {
      out_trade_no: id,
      body: "123",
      attach: "123"
    }, res => {
      console.log(res)
      that.requestPayment(res.data)
    })
  },

  requestPayment(payModel) {
    //  获取微信支付的数据
    let that = this;
    wx.requestPayment({
      'timeStamp': payModel.timeStamp,
      'nonceStr': payModel.nonceStr,
      'package': payModel.package,
      'paySign': payModel.paySign,
      'signType': "HMAC-SHA256",
      'success': function (res) {
        console.log('支付成功')
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/offlineApplySuccess/offlineApplySuccess?oid=' + that.data.exam_order_id,
          })
        }, 500)
        // that.getInit();
      },
      'fail': function (res) {
        console.log('支付失败');
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/myapply/myapply'
          })
        }, 500)
        // wx.showModal({
        //   title: '提示',
        //   content: '支付失败',
        //   showCancel: false,
        //   success: function (res) {
        //     if (res.confirm) {
        //       wx.reLaunch({
        //         url: '../order/order?oid=' + that.data.oid,
        //       })
        //     }
        //   }
        // });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      examItem: app.globalData.examItem,
      infoItem: app.globalData.infoItem,
    })
    console.log(this.data.infoItem)
    console.log(this.data.examItem)
    if (this.data.infoItem) {
      this.setData({
        isShowExaminee: false
      })
    }

    this.setData({
      price: this.data.examItem.offline_price
    })

    this.getVenuesList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})